﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.Services.Framework;
using GestionPedago.Services.ES;
using GestionPedago.Services.ES.Modele;
using GestionPedago.Services.Mapping;
using GestionPedago.EntityContext;
using Persistence.OPManager;
using GestionPedago.Persistence.OPManager;

namespace GestionPedago.Services.Traitement
{
    /// <summary>
    /// Classe de traitement du service RecupererPlanningECFDetail
    /// </summary>
    public class RecupererPlanningECFDetailTraitement : ITraitement<RecupererPlanningECFDetailIn, RecupererPlanningECFDetailOut>
    {
        /// <summary>
        /// Singleton
        /// </summary>
        private static RecupererPlanningECFDetailTraitement instance;
        
        /// <summary>
        /// Méthode GetInstance
        /// </summary>
        /// <returns>Le singleton</returns>
        public static RecupererPlanningECFDetailTraitement GetInstance()
        {
            if (instance == null)
            {
                instance = new RecupererPlanningECFDetailTraitement();
            }
            return instance;
        }

        /// <summary>
        /// Constructeur
        /// </summary>
        private RecupererPlanningECFDetailTraitement() { }

        /// <summary>
        /// Méthode de traitement
        /// </summary>
        /// <param name="paramEntree">Le paramètre d'entrée du service</param>
        /// <returns>Le paramètre de sortie du service</returns>
        public RecupererPlanningECFDetailOut Traitement(RecupererPlanningECFDetailIn paramEntree)
        {
            EntityConnection.Reinitialisation();
            EntityConnection.ClearEntities();
            List<PlanningECFDetailES> lst = new List<PlanningECFDetailES>();

           // PlanningIndividuelFormation pif = new PlanningIndividuelFormationMappeur().ES2Entity(paramEntree.PlanningIndividuelFormation);
            PlanningIndividuelFormation pif = PlanningIndividuelFormationOPManager.GetPlanningIndividuelFormationByCode(paramEntree.PlanningIndividuelFormation.CodePlanning);
           
            foreach (PlanningECFDetail r in PlanningECFDetailOPManager.GetListePlanningECFDetailByCodePlanningIndividuelFormation(pif.CodePlanning))
            {
                // Mapping du stagiaire Entity vers l'ES et ajout dans la liste
                PlanningECFDetailES pede = new PlanningECFDetailMappeur().Entity2ES(r);
                pede.DateSuggeree = pede.PlanningECF.DateECF;
                pede.DejaPlanifie = true;
                lst.Add(pede);
            }

            paramEntree.PlanningIndividuelFormation.ListePlanningIndividuelDetail.Sort(new PlanningIndividuelESComparer());

            foreach (ECF e in pif.GetListeECFAPasser())
            {
                PlanningECFDetailES pede = new PlanningECFDetailES();
                pede.PlanningECF = new PlanningECFES();
                pede.PlanningECF.ECF = new ECFMappeur().Entity2ES(e);
                pede.PlanningIndividuelFormation = paramEntree.PlanningIndividuelFormation;
                pede.DateSuggeree = e.GetSuggestionDate(pif);
                pede.SuggestionsPromotion = PromotionOPManager.GetListePromotionsSuggestionPlanification(pif.CodePlanning,e.IdECF);
                pede.DejaPlanifie = false;
                lst.Add(pede);
            }

            // Création de l'ES de sortie avec la liste obtenue
            return new RecupererPlanningECFDetailOut(lst);
        }
    }
}
